import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import pkg from './package.json'

import * as path from "path";
const { resolve } = require('path')
import electron from "vite-plugin-electron";
import electronRender from 'vite-plugin-electron-renderer' 
import legacy from '@vitejs/plugin-legacy';

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers' 

import libCss from 'vite-plugin-libcss';
// 引入等比适配插件
// const px2rem = require('postcss-px2rem')
// // 配置基本大小
// const postcss = px2rem({
//   // 基准大小 baseSize，需要和rem.js中相同
//   // remUnit: 14 代表 1rem = 14px; 所以当你一个14px值时，它会自动转成 (14px/14)rem
//   remUnit: 14
// })
// https://vitejs.dev/config/
export default defineConfig(({command}) => {
  const isServe = command === 'serve'
  const isBuild = command === 'build'
  const sourcemap = isServe || !!process.env.VSCODE_DEBUG

  return {
    base: process.env.NODE_ENV === 'production' ? './' : './',
    publicPath:".",
    publicDir: 'public',
    // cacheDir: 'node_modules/.vite',
    // css: {
    //   loaderOptions: {
    //     less: {
    //       javascriptEnabled: true,
    //     },
    //     postcss: {
    //       plugins: [
    //         postcss,
    //       ],
    //     },
    //   },
    // },
    plugins: [
      vue({
        template: {
          compilerOptions: {
            isCustomElement: (tag) =>
              (tag.startsWith('Tres') && tag !== 'TresCanvas') ||
              tag === 'primitive',
          },
        },
      }),//不写入vue就会报错
      libCss(),
      AutoImport ({
        imports: ["vue", "pinia", "vue-router"],
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      legacy({
        targets:["defaults","not IE 11"],
      }), 
      electron([{
        entry: "electron/index_linux.ts", // 主进程文件
        onstart(options) {
          if (process.env.VSCODE_DEBUG) {
            console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
          } else {
            options.startup()
          }
        },
        vite: {
          build: {
            sourcemap,
            minify: isBuild,
            outDir: 'dist-electron/main',
            rollupOptions: {
              external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
            },
          },
        },
      },
      {
        entry: 'electron/preload.js',
        onstart(options) {
          // Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete, 
          // instead of restarting the entire Electron App.
          options.reload()
        },
        vite: {
          build: {
            sourcemap: sourcemap ? 'inline' : undefined, // #332
            minify: isBuild,
            outDir: 'dist-electron',
            rollupOptions: {
              external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
            },
          },
        },
      }]),
      electronRender(),
    ], 
    build: {
      emptyOutDir: false, // 默认情况下，若 outDir 在 root 目录下，则 Vite 会在构建时清空该目录
      outDir: './dist',
      target: 'modules',
      assetsDir: 'assets',
      assetsInlineLimit: 360000, 
      cssCodeSplit:true, 
      // target: ['es2015', 'chrome63'], // 默认是modules,百度说是更改这个会去输出兼容浏览器，尝试没啥作用，先配置吧
    }, 
    resolve: {
      alias: {
        // "@": resolve(__dirname, "src"),
        // "@": path.resolve("./src"), // @代替src
        // 'views': path.resolve('src/views'),
        // 'utils': path.resolve('src/utils'),
        // 'assets': path.resolve('src/assets'),
        // 'api': path.resolve('src/api'),
      },
    },
    server: {
      open: true,
      port: 8080,
      host: "0.0.0.0",
      proxy: {
        '/app': {
          target: 'http://183.221.0.153:35009/app',//Axios跨域请求的IP
          changeOrigin: true,
          ws: true,
          secure: true,
          rewrite: (path) => path.replace(/^\/app/, ''),
        }
      } 
    },
    assetsInclude: ['**/**/*.json'],
    assetsInlineLimit: 0, 
  }
})
